package cn.maxwell.order.service.impl;

import cn.maxwell.basic.service.impl.BaseServiceImpl;
import cn.maxwell.basic.util.LoginContext;
import cn.maxwell.basic.util.PageList;
import cn.maxwell.order.domain.OrderPetAcquisition;
import cn.maxwell.order.dto.OrderDto;
import cn.maxwell.order.mapper.OrderPetAcquisitionMapper;
import cn.maxwell.order.query.OrderPetAcquisitionQuery;
import cn.maxwell.order.service.IOrderPetAcquisitionServer;
import cn.maxwell.org.domain.Employee;
import cn.maxwell.org.domain.Shop;
import cn.maxwell.org.mapper.EmployeeMapper;
import cn.maxwell.org.mapper.ShopMapper;
import cn.maxwell.pet.domain.Pet;
import cn.maxwell.pet.mapper.PetMapper;
import cn.maxwell.pet.mapper.SearchMasterMsgMapper;
import cn.maxwell.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionServer {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = (Logininfo) LoginContext.getLoginUser(request);
        Long logininfo_id = logininfo.getId();
        Employee employee = employeeMapper.loadByLogininfoId(logininfo_id);
        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shopId = employee.getShop_id();
        //2.店铺人员
        if (shopId!=null){
            Shop shop = shopMapper.loadById(shopId);
            if (shop.getAdmin_id()!=employee.getId()){
                //3.否则就为店员
                query.setEmp_id(employee.getId());
            }
            //4.employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员
            query.setShop_id(shopId);
        }
        //5.平台管理员
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto dto) {
        Long ordreId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();
        /**
         * 1. 改订单状态，金额, 支付方式
         * 2. 改寻主消息状态
         * 3. 改pet金额
         */
        // 1.改订单状态，金额, 支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(ordreId);
        orderPetAcquisition.setState(2);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 2.改寻主消息状态
        searchMasterMsgMapper.updateState(orderPetAcquisition.getSearch_master_msg_id());

        // 3.改pet金额
        Pet pet = petMapper.loadById(orderPetAcquisition.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    @Override
    public void cancel(Long id) {
        /**
         * 1. 修改订单状态
         * 2. 寻主消息的shopid=null
         * 3. pet 删除
         */
        // 1. 修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1); // 取消
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 2. 寻主消息的shopid=null
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearch_master_msg_id());

        // 3. pet 删除
        petMapper.delete(orderPetAcquisition.getPet_id());
    }
}
